
    /**  
    * @Title: W3CDocumentUtil.java
    * @Package com.efast.cafe.commponent.es.webservice.utils
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年10月19日
    * @version V1.0  
    */
    
package com.efast.cafe.commponent.es.webservice.utils;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.efast.cafe.util.JAXBUtil;

/**
    * @ClassName: W3CDocumentUtil
    * @Description: document节点与xml字符串之间的相互转换 对象与element之间的相互转换
    * @author xuanming.feng
    * @date 2018年10月19日
    *
    */

public class W3CElementUtil {

	public static String ElementToString(Element element){
		 try {
			 
			 	Document document = element.getOwnerDocument();
	            StringWriter sw = new StringWriter();
	            TransformerFactory tf = TransformerFactory.newInstance();
	            Transformer transformer = tf.newTransformer();
	            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
	            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
	            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
	            transformer.transform(new DOMSource(document), new StreamResult(sw));
	            return sw.toString();
	        } catch (Exception ex) {
	            throw new RuntimeException("Error converting to String", ex);
	        }
	}
	
	
	public static Element StringToElement(String xmlString){
		try {
			
			Source source = new StreamSource(new StringReader(xmlString));
			DOMResult result = new DOMResult();
			TransformerFactory.newInstance().newTransformer().transform(source , result);
			Document doc = (Document) result.getNode();
			return doc.getDocumentElement();
		 } catch (Exception ex) {
	            throw new RuntimeException("Error converting to Element", ex);
	     }
	}
	
	/**
	    * @Title: elementUnmarshalToObj
	    * @Description: Element转对象
	    * @param @param element
	    * @param @param type
	    * @param @return    参数
	    * @return T    返回类型
	    * @throws
	 */
	public static <T> T elementUnmarshalToObj(Element element,Class<T> type){
		String xmlcontent = ElementToString(element);
		return JAXBUtil.unmarshal(xmlcontent, type);
	}
	
	/**
	    * @Title: elementUnmarshalToObj
	    * @Description: Element转对象
	    * @param @param element
	    * @param @param contentEncoding
	    * @param @param type
	    * @param @return    参数
	    * @return T    返回类型
	    * @throws
	 */
	public static <T> T elementUnmarshalToObj(Element element,String contentEncoding,Class<T> type){
		String xmlcontent = ElementToString(element);
		return JAXBUtil.unmarshal(xmlcontent,contentEncoding, type);
	}
	
	/**
	 * 
	    * @Title: ObjmarshalToElement
	    * @Description: 对象转element
	    * @param @param obj
	    * @param @return
	    * @param @throws JAXBException    参数
	    * @return Element    返回类型
	    * @throws
	 */
	public static Element ObjmarshalToElement(Object obj)throws JAXBException{
		String xml = JAXBUtil.marshalObject(obj);
		return StringToElement(xml);
	}
	
	/**
	 * 
	    * @Title: ObjmarshalToElement
	    * @Description: 对象转element
	    * @param @param obj
	    * @param @param classes
	    * @param @return
	    * @param @throws JAXBException    参数
	    * @return Element    返回类型
	    * @throws
	 */
	public static Element ObjmarshalToElement(Object obj,Class<?>[] classes)throws JAXBException{
		String xml = JAXBUtil.marshalObject(obj,classes);
		return StringToElement(xml);
	}
}
